/**
* This file is part of OntoCompoFramework
*
* Copyright (C) 2011 -  Christian Brel
*
* @author   Main    Christian Brel
**/
package ontocompo.ui.screenmanagement;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

import ontocompo.ui.appmanagement.Application;
import ontocompo.ui.appmanagement.ApplicationManager;
import ontocompo.ui.global.Resources;
import ontocompo.ui.screenmanagement.Screen;
import ontocompo.ui.selection.SelectedComponent;
import ontocompo.ui.selection.Selector;

public class ScreenFlowSelector extends Selector {

	private JComponent selectedButton;
	private Border borderBak;
	private Color bgBak;
	private boolean opaqueBak;
	
	public ScreenFlowSelector() {
		super();
	}
	
	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		JComponent over = (JComponent) arg0.getSource();
		
		boolean different = false;
		
		if(selectedButton != null) {
			if(! selectedButton.equals(over)) {
				different = true;
			}
			selectedButton.setBorder(this.borderBak);
			selectedButton.setBackground(this.bgBak);
			selectedButton.setOpaque(this.opaqueBak);
			
			this.selectedButton = null;
		} else {
			different = true;
		}
		
		if(different) {
			this.selectedButton = over;
			this.borderBak = over.getBorder();
			this.bgBak = over.getBackground();
			this.opaqueBak = over.isOpaque();
			
			over.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED, Color.red, Color.yellow));
			over.setBackground(this.selectedColor);
			over.setOpaque(true);
			
			this.setChanged();
			this.notifyObservers("enable");
		} else {
			this.setChanged();
			this.notifyObservers("disable");
		}
	}

	public void selectButton(JComponent over) {
		if(selectedButton != null) {
			selectedButton.setBorder(this.borderBak);
			selectedButton.setBackground(this.bgBak);
			selectedButton.setOpaque(this.opaqueBak);
			
			this.selectedButton = null;
		}

		this.selectedButton = over;
		this.borderBak = over.getBorder();
		this.bgBak = over.getBackground();
		this.opaqueBak = over.isOpaque();
		
		over.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED, Color.red, Color.yellow));
		over.setBackground(this.selectedColor);
		over.setOpaque(true);
		
		this.setChanged();
		this.notifyObservers("enable");
	}
	
	@Override
	public void mouseEntered(MouseEvent arg0) {
		super.mouseEntered(arg0);
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		JComponent over = (JComponent) arg0.getSource();
		
		if(selectedButton == null || !over.equals(selectedButton)) {
			super.mouseExited(arg0);
		}
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		//super.mousePressed(arg0);
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		//super.mouseReleased(arg0);
	}
	
	public JComponent getSelectedButton() {
		return this.selectedButton;
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		
	}

}
